package ibmetp.mis.dao;
import ibmetp.mis.vo.Page;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * user Dao
 * @author LAN DABIN
 * @create 2011-9-29
 * @version 1.0
 * @updated 2011-9-30 
 */
public abstract class BaseDao {
	protected Connection conn;

	/**
	 * 添加条件
	 * 
	 * @param o   查询的模糊对象
	 *         
	 * @return 返回条件
	 */
	public abstract Map<Integer, Object[]> getCondition(Object o);

	/**
	 * 将查询结果转换成实体类列表
	 * 
	 * @param rs
	 *            查询结果
	 * @return 
	 *   		实体类列表
	 */
	public abstract List ResultSet2List(ResultSet rs);

	/**
	 * 根据查询件动态生成PreparedStatement对象
	 * 
	 * @param querySQL
	 *            查询sql
	 * @param paramsMap
	 *            保存的动态查询条件map
	 * @return
	 * @throws SQLException
	 */
	private PreparedStatement getPreparedStatement(String querySQL,
			Map<Integer, Object[]> paramsMap) throws SQLException {
		String conditionString = "";

		Set<Integer> keys = paramsMap.keySet();
		for (Integer key : keys) {
			Object[] condition = paramsMap.get(key);
			if (condition != null) {
				conditionString += condition[0];
			}
		}
		querySQL += conditionString;
		conn = DBUtil.getConnection();
		System.out.println(querySQL);
		PreparedStatement ps = conn.prepareStatement(querySQL);
		for (Integer key : keys) {
			Object[] condition = paramsMap.get(key);
			if (condition != null) {
				ps.setObject(key, condition[1]);
			}
		}
		return ps;
	}

	/**
	 * 返回查询结果的某一页面的信息
	 * 
	 * @param user
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page find(Object o, int pageNo, int pageSize, String tableName) {

		Page page = new Page();
		int total;
		String querySQL = "select * from (select a.*, rownum rn from "
				+ tableName + " a where 1=1 ";

		String queryCount = "select count(*)  from  " + tableName
				+ " WHERE 1=1";
		System.out.println(querySQL);

		Map<Integer, Object[]> paramsMap = getCondition(o);
		/*
		 * 动态取得查询条�?
		 */
		String sqlString = getSQL(querySQL, paramsMap);
		sqlString += " and rownum<= " + pageNo * pageSize + " ) where rn>"
				+(pageNo - 1) * pageSize;
		System.out.println(sqlString);

		try {
			PreparedStatement ps = getPreStatement(sqlString, paramsMap);
			ResultSet rs = ps.executeQuery();
			List resultList = ResultSet2List(rs);
			ps = getPreparedStatement(queryCount, paramsMap);
			rs = ps.executeQuery();
			rs.next();
			total = rs.getInt(1);

			System.out.println("total:" + total);
			//System.out.println(resultList);
			ps.close();
			conn.close();
			page.setList(resultList);
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			page.setTotal(total);
			return page;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page;
	}

	/**
	 * 构造sql
	 * 
	 * @param querySQL
	 * @param paramsMap
	 * @return
	 */
	public String getSQL(String querySQL, Map<Integer, Object[]> paramsMap) {
		String conditionString = "";

		Set<Integer> keys = paramsMap.keySet();
		for (Integer key : keys) {
			Object[] condition = paramsMap.get(key);
			if (condition != null) {
				conditionString += condition[0];
			}
		}
		querySQL += conditionString;
		return querySQL;
	}

	/**
	 * 设置prestatement sql参数
	 * 
	 * @param querySQL
	 * @param paramsMap
	 * @return
	 * @throws SQLException
	 */
	public PreparedStatement getPreStatement(String querySQL,
			Map<Integer, Object[]> paramsMap) throws SQLException {
		Set<Integer> keys = paramsMap.keySet();
		conn = DBUtil.getConnection();
		System.out.println(querySQL);
		PreparedStatement ps = conn.prepareStatement(querySQL);
		for (Integer key : keys) {
			Object[] condition = paramsMap.get(key);
			if (condition != null) {
				ps.setObject(key, condition[1]);
			}
		}
		return ps;
	}

}
